package com.oracle.lingshangejia.activity;

import android.annotation.SuppressLint;
import android.os.PersistableBundle;
import android.support.annotation.Nullable;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentTransaction;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;
import android.widget.Toast;

import com.gyf.barlibrary.ImmersionBar;
import com.lws.basic.utils.common.SharePreferenceUtils;
import com.oracle.lingshangejia.Constant;
import com.oracle.lingshangejia.MyApp;
import com.oracle.lingshangejia.base.BaseActivity;
import com.oracle.lingshangejia.fragment.FileFragment;
import com.oracle.lingshangejia.fragment.HomeFragment;
import com.oracle.lingshangejia.fragment.MarkFragment;
import com.oracle.lingshangejia.fragment.MyFragment;

import com.oracle.lingshangejia.R;


import io.agora.AgoraAPI;
import io.agora.IAgoraAPI;
import io.agora.rtc.RtcEngine;


/**
 *
 * 首页社区医生
 */
public class MainActivity extends BaseActivity implements View.OnClickListener {
    /**
     * 首页 档案 问询 我的
     */
    private static long lastBackTime = 0;//上一次点击 back 键的时间,用于双击退出的判断
    private final int BACK_INTERVAL = 1000;//当双击 back 键在此间隔内是直接触发 onBackPressed
    private LinearLayout ll_home,ll_file,ll_mark,ll_my;
    private ImageView iv_home,iv_file,iv_mark,iv_my;
    private TextView tv_home,tv_file,tv_mark,tv_my;
    private LinearLayout ll_main;
    private FragmentManager fragmentManager;
    private FragmentTransaction beginTransaction;
    private HomeFragment homeFragment;
    private FileFragment fileFragment;
    private MarkFragment markFragment;
    private MyFragment myFragment;
    private String appId;
    @Override
    public int intiLayout() {
        return R.layout.activity_main;
    }
    @SuppressLint("MissingSuperCall")
    @Override
    protected void onSaveInstanceState(Bundle outState) {
        //解决时间久了fragment不切换的问题
    }
    @Override
    public void initView() {
        ll_home=findViewById(R.id.ll_home);
        ll_file=findViewById(R.id.ll_file);
        ll_mark=findViewById(R.id.ll_mark);
        ll_my=findViewById(R.id.ll_my);
        iv_home=findViewById(R.id.iv_home);
        iv_file=findViewById(R.id.iv_file);
        iv_mark=findViewById(R.id.iv_mark);
        iv_my=findViewById(R.id.iv_my);
        tv_home=findViewById(R.id.tv_home);
        tv_file=findViewById(R.id.tv_file);
        tv_mark=findViewById(R.id.tv_mark);
        tv_my=findViewById(R.id.tv_my);
        ll_main=findViewById(R.id.ll_main);
        setOnClick();
        fragmentManager = getSupportFragmentManager();//获取到fragment的管理对象
        setClick(0);//默认进去显示homefragment
        selecthome();
        SharePreferenceUtils.putString(this, "base_url", Constant.baseurl);
    }

    /**
     * 默认首页
     * @param i
     */
    private void setClick(int i) {
        ImmersionBar mImmersionBar = ImmersionBar.with(this);
        //开启事务
        beginTransaction = fragmentManager.beginTransaction();
        //显示之前将所有的fragment都隐藏起来,在去显示我们想要显示的fragment
        hideFragment(beginTransaction);
        switch (i) {
            case 0://主页
                if (homeFragment == null) {
                    homeFragment = new HomeFragment();
                    //加入事务
                    beginTransaction.add(R.id.ll_main, homeFragment);
                } else {

                    beginTransaction.show(homeFragment);
                }
                break;
            case 1: //档案
                if (fileFragment == null) {
                    fileFragment = new FileFragment();
                    //加入事务
                    beginTransaction.add(R.id.ll_main, fileFragment);
                } else {
                    //如果fragment不为空就显示出来
                    beginTransaction.show(fileFragment);
                }
                break;
            case 2: //问询
                if (markFragment == null) {
                    markFragment = new MarkFragment();
                    //加入事务
                    beginTransaction.add(R.id.ll_main, markFragment);
                } else {
                    //如果fragment不为空就显示出来
                    beginTransaction.show(markFragment);
                }
                break;
            case 3: //我的
                if (myFragment == null) {
                    myFragment = new MyFragment();
                    //加入事务
                    beginTransaction.add(R.id.ll_main, myFragment);
                } else {
                    //如果fragment不为空就显示出来
                    beginTransaction.show(myFragment);
                    myFragment.getData();
                }
                break;
        }
        mImmersionBar
                .statusBarColor(R.color.colorBlue)
                .statusBarDarkFont(true)
                .fitsSystemWindows(true).init();   //所有子类都将继承这些相同的属性
        //提交事务
        beginTransaction.commit();
    }
    /**
     * 点击
     */
    private void setOnClick() {
        ll_home.setOnClickListener(this);
        ll_file.setOnClickListener(this);
        ll_mark.setOnClickListener(this);
        ll_my.setOnClickListener(this);
    }
    /**
     * 用来隐藏fragment的方法
     *
     * @param fragmentTransaction
     */
    private void hideFragment(FragmentTransaction fragmentTransaction) {
        //如果此fragment不为空的话就隐藏起来
        if (homeFragment != null) {
            fragmentTransaction.hide(homeFragment);
        }
        if (fileFragment != null) {
            fragmentTransaction.hide(fileFragment);
        }
        if (markFragment != null) {
            fragmentTransaction.hide(markFragment);
        }
        if (myFragment != null) {
            fragmentTransaction.hide(myFragment);
        }
    }
    @Override
    public void initData() {

    }

    @Override
    public void onClick(View v) {
        switch (v.getId()){
            case R.id.ll_home:
                setClick(0);
                selecthome();
                break;
            case R.id.ll_file:
                setClick(1);
                selectfile();
                break;
            case R.id.ll_mark:
                setClick(2);
                selectmark();
                break;
            case R.id.ll_my:
                setClick(3);
                selectmy();
                break;
        }
    }

    /**
     * 首页
     */
    private  void selecthome(){
        iv_home.setSelected(true);
        tv_home.setSelected(true);
        iv_file.setSelected(false);
        tv_file.setSelected(false);
        iv_mark.setSelected(false);
        tv_mark.setSelected(false);
        iv_my.setSelected(false);
        tv_my.setSelected(false);
    }

    /**
     * 我的
     */
    public  void selectmy(){
        iv_my.setSelected(true);
        tv_my.setSelected(true);
        iv_home.setSelected(false);
        tv_home.setSelected(false);
        iv_file.setSelected(false);
        tv_file.setSelected(false);
        iv_mark.setSelected(false);
        tv_mark.setSelected(false);
    }

    /**
     * 档案
     */
    public  void selectfile(){
        iv_file.setSelected(true);
        tv_file.setSelected(true);
        iv_home.setSelected(false);
        tv_home.setSelected(false);
        iv_my.setSelected(false);
        tv_my.setSelected(false);
        iv_mark.setSelected(false);
        tv_mark.setSelected(false);
    }

    /**
     * 问询
     */
    public  void selectmark(){
        iv_mark.setSelected(true);
        tv_mark.setSelected(true);
        iv_home.setSelected(false);
        tv_home.setSelected(false);
        iv_my.setSelected(false);
        tv_my.setSelected(false);
        iv_file.setSelected(false);
        tv_file.setSelected(false);
    }
    private void addCallback() {

        MyApp.the().getmAgoraAPI().callbackSet(new AgoraAPI.CallBack() {

            @Override
            public void onLoginSuccess(int i, int i1) {
                Log.i(TAG, "onLoginSuccess " + i + "  " + i1);
                runOnUiThread(new Runnable() {
                    @Override
                    public void run() {

                    }
                });
            }

            @Override
            public void onLoginFailed(final int i) {
                Log.i(TAG, "onLoginFailed " + i);
                runOnUiThread(new Runnable() {
                    @Override
                    public void run() {
                        if (i == IAgoraAPI.ECODE_LOGIN_E_NET) {
                            //  ToastUtils.show(new WeakReference<Context>(LoginActivity.this), getString(R.string.str_msg_net_bad));
                        }
                    }
                });
            }


            @Override
            public void onError(String s, int i, String s1) {
                Log.i(TAG, "onError s:" + s + " s1:" + s1);
            }

        });
    }
    @Override
    protected void onResume() {
        super.onResume();

    /*    String username= (String) SpUtils.get(MainActivity.this,"s_phone","");
        if (MyApp.the().getmAgoraAPI() != null){
            if ( MyApp.the().getmAgoraAPI().getStatus() != 2){
               MyApp.the().getmAgoraAPI().login2(getString(R.string.agora_app_id), username, "_no_need_token", 0, "", 5, 1);

            }
            addCallback();
            StringBuffer version = new StringBuffer("" + MyApp.the().getmAgoraAPI().getSdkVersion());
            if (version.length() >= 8) {
                String strVersion = "version " + version.charAt(2) + "." + version.charAt(4) + "." + version.charAt(6);
                Log.e("version",strVersion);
            }
        }*/

    }
    @Override
    protected void onDestroy() {
        super.onDestroy();
        Log.i(TAG, "onDestroy");
        RtcEngine.destroy();

        MyApp.logout();
    }

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        if (savedInstanceState != null){

        }
    }

    @Override
    public void onSaveInstanceState(Bundle outState, PersistableBundle outPersistentState) {
        outState.putInt("date",1 );
        super.onSaveInstanceState(outState, outPersistentState);

    }
    @Override
    public void onBackPressed() {
        long currentTime = System.currentTimeMillis();
        if (currentTime - lastBackTime < BACK_INTERVAL) {
            super.onBackPressed();
        } else {
            Toast.makeText(this, "双击 back 退出", Toast.LENGTH_SHORT).show();
        }
        lastBackTime = currentTime;
    }
}
